package com.yujing.yview

import android.app.Activity
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.yujing.yview.base.YBaseDialog
import com.yujing.yview.databinding.DialogTipsBinding

/**
 * 提示对话框
 */
/*
用法：
YTipsDialog(this).apply {
    title = "通知"
    content = "这是一条提示消息!"
    btNameOK = "我知道了"
    showCancelButton = false
    setOnClickListener {
        dismiss()
        //确定逻辑
    }
    show()
}

YTipsDialog(this, false, "提示", "是否确定？").apply {
    textSizeScale = 2.0f
    btNameOK = "确定"
    setOnClickListener {
        dismiss()
        //确定逻辑
    }
    btNameCancel = "取消"
    setCancelOnClickListener {
        dismiss()
        //取消取消
    }
    show()
}
 */
class YTipsDialog : YBaseDialog<DialogTipsBinding> {
    private var onClickListener: View.OnClickListener? = null
    private var cancelOnClickListener: View.OnClickListener? = null
    var title: String? = null
    var content: String? = null
    var showCancelButton = true
    var btNameOK = "确　定"
    var btNameCancel = "取　消"

    //文字大小比例
    var textSizeScale = 1.0f

    //初始化完成
    var initCompleteListener: ((DialogTipsBinding) -> Unit)? = null

    constructor(activity: Activity) : super(activity, R.layout.dialog_tips, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar) {
        fullscreen = true
        fillColor = ContextCompat.getColor(activity, R.color.fill_background)
    }

    constructor(activity: Activity, isCancelable: Boolean, title: String?, content: String?) : super(activity, R.layout.dialog_tips, android.R.style.Theme_DeviceDefault_Dialog_NoActionBar) {
        cancel = isCancelable
        this.title = title
        this.content = content
        fullscreen = true
        fillColor = ContextCompat.getColor(activity, R.color.fill_background)
    }

    override fun init() {
        //设置标题
        binding.tvTitle.text = title
        binding.tvContent.text = content
        //设置确定按钮
        binding.btOk.text = btNameOK
        binding.btOk.setOnClickListener(onClickListener)
        if (!showCancelButton) {
            binding.btCancel.visibility = View.GONE
            binding.btOk.setBackgroundResource(R.drawable.button)
        } else {
            binding.btCancel.visibility = View.VISIBLE
            binding.btOk.setBackgroundResource(R.drawable.button_right)
        }
        //设置取消按钮
        binding.btCancel.text = btNameCancel
        binding.btCancel.setOnClickListener { v -> if (cancelOnClickListener == null) dismiss() else cancelOnClickListener!!.onClick(v) }

        //---------------------------缩放---------------------------
        //设置文字放大缩小倍数
        binding.tvTitle.textSize = px2sp(binding.tvTitle.textSize.toInt()) * textSizeScale
        binding.tvContent.textSize = px2sp(binding.tvContent.textSize.toInt()) * textSizeScale
        binding.btCancel.textSize = px2sp(binding.btCancel.textSize.toInt()) * textSizeScale
        binding.btOk.textSize = px2sp(binding.btOk.textSize.toInt()) * textSizeScale
        //按钮高度缩放
        val lp = binding.llBt.layoutParams as LinearLayout.LayoutParams
        lp.height = (lp.height * textSizeScale).toInt()
        binding.llBt.layoutParams = lp
        //---------------------------缩放---------------------------
        initCompleteListener?.invoke(binding)
    }

    fun setOnClickListener(onClickListener: View.OnClickListener?): YTipsDialog {
        this.onClickListener = onClickListener
        return this
    }

    fun setCancelOnClickListener(cancelOnClickListener: View.OnClickListener?) {
        this.cancelOnClickListener = cancelOnClickListener
    }
}